﻿<Page x:Class="VRacer.Pages.GamePage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:VRacer"
      xmlns:controls="clr-namespace:VRacer.Controls"
      xmlns:converters="clr-namespace:VRacer.Client.Converters"
      xmlns:common="clr-namespace:VRacer;assembly=VRacer"
      xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
      Name="Root" Title="Game" >

    <!-- 
      "-->

    <Page.Resources>
        <converters:ChatBoxVisibilityConverter x:Key="ChatBoxVisibilityConverter" />

        <Style x:Key="InfoBorderStyle" TargetType="GroupBox">
            <Setter Property="BorderThickness" Value="2" />
            <Setter Property="Padding" Value="10" />
            <Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush Color="White" Opacity="0.2" />
                </Setter.Value>
            </Setter>
            <Setter Property="MinHeight" Value="100" />
            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock FontWeight="Bold" Text="{Binding}" />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>


        <Style x:Key="InitializedBorderStyle" TargetType="GroupBox">
            <Setter Property="BorderBrush" Value="Black" />
            <Setter Property="BorderThickness" Value="2" />
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="1,0" Opacity="0.9">
                        <GradientStop Color="#eeeefa" Offset="0" />
                        <GradientStop Color="#eaeaff" Offset="0.6" />
                        <GradientStop Color="#eeeefa" Offset="1" />
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="Padding" Value="15,5,15,5" />
            <Setter Property="Margin" Value="10" />
            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock FontWeight="Bold" Text="{Binding}" />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <CollectionViewSource Source="{Binding ElementName=Root, Path=Messages}" x:Key="messages">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Time.Ticks" Direction="Descending"/>
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

        <DataTemplate DataType="{x:Type common:Score}">
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding StringFormat="{}{0} ({1})">
                        <Binding Path="Player" />
                        <Binding Path="Result" />
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>

        <DataTemplate DataType="{x:Type common:Message}">
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding StringFormat="{}{0}: {1}">
                        <Binding Path="From" />
                        <Binding Path="Text" />
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>

        <DataTemplate DataType="{x:Type common:PlayerBase}">
            <TextBlock Foreground="{Binding BrushColor}" Name="_title" Text="{Binding Title}" />

            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding IsActive}" Value="True">
                    <Setter Property="FontWeight" Value="Bold" TargetName="_title" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <DataTemplate DataType="{x:Type common:Game}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Border Grid.Column="0" BorderThickness="2" Padding="15,5,15,5">
                    <Border.BorderBrush>
                        <SolidColorBrush Opacity="0.1" Color="Black" />
                    </Border.BorderBrush>
                    <Border.Background>
                        <RadialGradientBrush Center="0,0" RadiusX="1" RadiusY="1.5" GradientOrigin="0,0">
                            <GradientStop Color="#fafafa" Offset="0"/>
                            <GradientStop Color="#dedefe" Offset="0.971"/>
                        </RadialGradientBrush>
                    </Border.Background>
                    <Border MinWidth="200">
                        <DockPanel>
                            <GroupBox DockPanel.Dock="Top" Name="_infoPanel" Style="{StaticResource InfoBorderStyle}" Header="Game">
                                <StackPanel>
                                    <TextBlock Text="{Binding ActivePlayer.Title, StringFormat='{}Player: {0}'}" />
                                    <TextBlock Text="{Binding Laps, StringFormat='{}Laps: {0}'}" />
                                    <TextBlock Text="{Binding CurrentTurn, StringFormat='{}Turn: {0}'}" />
                                </StackPanel>
                            </GroupBox>
                            <GroupBox DockPanel.Dock="Top" Name="_playerPanel" Style="{StaticResource InfoBorderStyle}" Header="Player" DataContext="{Binding ActivePlayer}">
                                <StackPanel>
                                    <TextBlock Text="{Binding Title}" FontWeight="Bold" Foreground="{Binding BrushColor}" />
                                    <TextBlock Text="{Binding Laps, StringFormat='{}Lap: {0}'}" />
                                    <TextBlock Text="{Binding Statistics.Moves, StringFormat='{}Moves: {0}'}" />
                                    <TextBlock Text="{Binding Path='(common:ITimeStatistics.Statistics).AverageTimeForTurn', StringFormat='{}Average turn: {0} sec'}" />
                                    <TextBlock Text="{Binding Path='(common:ITimeStatistics.Statistics).LastTurnTime', StringFormat='{}Last turn: {0} sec'}" />
                                    <TextBlock Text="{Binding Path='(common:ITimeStatistics.Statistics).TotalTurnTime', StringFormat='{}Total time: {0} sec'}" />
                                </StackPanel>
                            </GroupBox>
                            <GroupBox DockPanel.Dock="Top" Name="_playersPanel" Style="{StaticResource InfoBorderStyle}" Header="Players">
                                <ItemsControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Players}" />
                            </GroupBox>
                            <GroupBox DockPanel.Dock="Top" Style="{StaticResource InfoBorderStyle}" Header="Messages">
                                <ItemsControl x:Name="_chatMessages" ItemsSource="{Binding Source={StaticResource messages}}" />
                            </GroupBox>
                        </DockPanel>
                    </Border>
                </Border>



                <Grid Grid.Column="1" HorizontalAlignment="Stretch">

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    
                    <TextBlock Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20" FontWeight="Bold" Text="{Binding CurrentTrack.Title}" />

                    <Border Grid.Row="1" Padding="15,5,15,5" Margin="5">

                            <Viewbox StretchDirection="Both" Stretch="Uniform">
                                <controls:GameBoard Game="{Binding}" />
                            </Viewbox>
                    </Border>

                    <GroupBox Grid.Column="1" Grid.Row="2" Visibility="{Binding Converter={StaticResource ChatBoxVisibilityConverter}}"  Style="{StaticResource InfoBorderStyle}" Header="Chat">
                        <TextBox KeyDown="TextBox_KeyDown" />
                    </GroupBox>

                    <!-- MASKA -->

                    <Border Grid.RowSpan="2" Visibility="Collapsed" Name="_initializedMask" MouseDown="Border_MouseDown">
                        <Border.Background>
                            <SolidColorBrush Color="White" Opacity="0.8" />
                        </Border.Background>

                        <TextBlock Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20" FontWeight="Bold">Kliknij aby rozpocząć...</TextBlock>

                        <!--<StackPanel>
                            <GroupBox Style="{StaticResource InitializedBorderStyle}" Header="Game">
                                <StackPanel>
                                    <TextBlock Text="{Binding Players.Count, StringFormat='{}Players : {0}'}" />
                                    <TextBlock Text="{Binding Laps, StringFormat='{}Laps: {0}'}" />
                                </StackPanel>
                            </GroupBox>
                            <GroupBox Style="{StaticResource InitializedBorderStyle}" Header="Track">
                                <ContentControl Content="{Binding CurrentTrack}" />
                            </GroupBox>
                            <GroupBox Style="{StaticResource InitializedBorderStyle}" Header="Players in game">
                                <ItemsControl ItemsSource="{Binding Players}" />
                            </GroupBox>
                            
                        </StackPanel>-->
                    </Border>

                    <Border Grid.RowSpan="2" Visibility="Collapsed" Name="_finishedMask">
                        <Border.Background>
                            <SolidColorBrush Color="White" Opacity="0.8" />
                        </Border.Background>

                        <StackPanel>
                            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20" FontWeight="Bold">Game finished!</TextBlock>
                            <GroupBox Style="{StaticResource InitializedBorderStyle}" Header="Track">
                                <ContentControl Content="{Binding CurrentTrack}" />
                            </GroupBox>
                            <GroupBox Style="{StaticResource InitializedBorderStyle}" Header="Scores">
                                <ItemsControl ItemsSource="{Binding Scores}" />
                            </GroupBox>
                        </StackPanel>
                    </Border>

                    <Border Grid.RowSpan="2" Visibility="Collapsed" Name="_uninitializedMask" Background="White">
                        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20" FontWeight="Bold">Connecting...</TextBlock>
                    </Border>

                </Grid>


            </Grid>

            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding State}" Value="Uninitialized">
                    <Setter Property="Visibility" TargetName="_uninitializedMask" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding State}" Value="Initialized">
                    <Setter Property="Visibility" TargetName="_initializedMask" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding State}" Value="Finished">
                    <Setter Property="Visibility" TargetName="_finishedMask" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding State}" Value="Ended">
                    <Setter Property="Visibility" TargetName="_finishedMask" Value="Visible" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

    </Page.Resources>

    <DockPanel>
        <StatusBar DockPanel.Dock="Bottom">
            <StatusBarItem>
                <TextBlock Text="{Binding State, StringFormat='{}State: {0}'}" />
            </StatusBarItem>

            <StatusBarItem>
                <TextBlock Text="{Binding Connection.IsActive, StringFormat='{}Remote game {0}'}" />
            </StatusBarItem>
        </StatusBar>

        <ContentControl Content="{Binding}" />
    </DockPanel>
</Page>
